/*
   Copyright 2009 Bégaudeau Stéphane

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package faneclipse.editors;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWhitespaceDetector;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WhitespaceRule;
import org.eclipse.jface.text.rules.WordRule;

/**
 * The FanCodeScanner class is in charge of scanning the code (default) partition
 * and putting the right token when a mart of the code partition respect a specific
 * rule like a keyword or a "string".
 * @author BEGAUDEAU stephane
 */
public class FanCodeScanner extends RuleBasedScanner {
	/**
	 * Array of the keyword of the Fan programming language.
	 */
	public static final String[] KEYWORD = new String[] {
		"abstract", "as", "break", "case", "catch", "class", "const", "continue", "default", "do", "else",
		"enum", "false", "final", "finally", "for", "foreach", "if", "internal", "is", "isnot",
		"mixin", "native", "new", "null", "once", "override", "private", "protected", "public",
		"readonly", "return", "static", "super", "switch", "this", "throw", "true", "try", "using",
		"virtual", "volatile", "Void", "while"};
	
	/**
	 * The detector that check if a word is valid (for coloration).
	 */
	private FanWordDetector fanWordDetector = new FanWordDetector();
	
	/**
	 * The constructor.
	 * @param provider The provider of every attributes for the syntax highlighting.
	 */
	public FanCodeScanner(final FanTextAttributeProvider provider) {
		List<IRule> rules = new ArrayList<IRule>();
		IToken keywordToken = new Token(provider.getAttribute(FanTextAttributeProvider.KEYWORD_ATTRIBUTE));
		IToken defaultToken = new Token(provider.getAttribute(FanTextAttributeProvider.DEFAULT_ATTRIBUTE));
		IToken stringToken = new Token(provider.getAttribute(FanTextAttributeProvider.STRING_ATTRIBUTE));
		
		rules.add(new WhitespaceRule(new IWhitespaceDetector() {
			public boolean isWhitespace(final char c) {
				return Character.isWhitespace(c);
			}
		})
		);
		
		// Rules for strings "a string"
		rules.add(new MultiLineRule("\"", "\"", stringToken));
		
		// Rules for the keyword detection we link each keyword to the keyword token.
		WordRule wr = new WordRule(fanWordDetector, defaultToken);
		for(int i = 0; i < KEYWORD.length; ++i) {
			wr.addWord(KEYWORD[i], keywordToken);
		}
		rules.add(wr);
		
		IRule[] param = new IRule[rules.size()];
		rules.toArray(param);
		setRules(param);
	}
}
